package com.google.code.drift.utils;

import android.content.Context;
import android.content.Intent;

public class CommonUtils {

	private CommonUtils() {
	};

	public static final int START_PROGRESS = 0;

	public static final int LOGIN_SUCCESS = 1;

	public static final int REGISTER_SUCCESS = 2;

	public static final int LOGIN_TIMEOUT = 3;

	public static final int LOGIN_ERROR = 4;

	public static final int CHANGE_SUCCESS = 5;

	public static final int CHANGE_TIMEOUT = 6;

	public static final int CHANGE_ERROR = 7;

	public static final int NOTIFY_ONCE_ACTION = 8;

	public static final int CHAT_ACTIVITY_JOIN_ROOM = 9;

	public static final int CHAT_ACTIVITY_QUIT_ROOM = 10;

	public static final String ACTION_SEND_MESSAGE = "action_send_message";

	public static final String ACTION_SEND_MESSAGE_FOR_TIMEOUT = "action_send_message_for_timeout";

	public static final String ACTION_CHAT_MESSAGE_RECEIVER = "action_chat_message_receive";

	/**
	 * 判断字符串是否不为空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNotNullString(String s) {
		if (s == null) {
			return false;
		}
		s = s.trim();
		if (s.equals("") || s.equals("null")) {
			return false;
		}
		return true;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNullString(String s) {
		return !isNotNullString(s);
	}

	/**
	 * 要发送一个数据到服务器
	 * 
	 * @param context
	 * @param data
	 */
	public static void sendDataToServer(Context context, String data) {
		toBroadcast(context, ACTION_SEND_MESSAGE, data);
	}

	/**
	 * 要发送一个数据到服务器，有超时限制
	 * 
	 * @param context
	 * @param data
	 */
	public static void sendDataToServerForTimeout(Context context, String data,
			int timeout) {
		toBroadcast(context, ACTION_SEND_MESSAGE_FOR_TIMEOUT, data, timeout);
	}

	/**
	 * 收到一个聊天回复，广播给监听该事件的类
	 * 
	 * @param context
	 * @param chatData
	 */
	public static void receiveChatDataFromServer(Context context,
			String chatData) {
		toBroadcast(context, ACTION_CHAT_MESSAGE_RECEIVER, chatData);
	}

	/**
	 * 一个发送广播的工具函数
	 * 
	 * @param context
	 * @param action
	 * @param jsonData
	 */
	private static void toBroadcast(Context context, String action,
			String jsonData) {
		Intent intent = new Intent(action);
		intent.putExtra("json", jsonData);
		context.sendBroadcast(intent);
	}

	/**
	 * 发送一个广播，并且带有socket超时时间
	 * @param context
	 * @param action
	 * @param jsonData
	 * @param timeout
	 */
	private static void toBroadcast(Context context, String action,
			String jsonData, int timeout) {
		Intent intent = new Intent(action);
		intent.putExtra("json", jsonData);
		intent.putExtra("timeout", timeout);
		context.sendBroadcast(intent);
	}

}
